#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <stdio.h>

#include "../includes/clientInfo.h"

// 初始化链表
void initClientList(clientList* list) {
    list->total = 0;
    list->listenfd = 0;
    list->head = NULL;
    list->tail = NULL;
	bzero(&list->server,sizeof(struct sockaddr_in));					                	
}

// 创建新客户端
newClient* createNewClient(int connfd,struct sockaddr_in clientData) {
    newClient* client = (newClient*)malloc(sizeof(newClient));
    if (!client) {
        perror("新客户端信息存储空间创建失败\n");
        exit(1);
    }
    //新客户端的有关数据                     
    client->connfd = connfd;
    client->clientData = clientData;
    client->prev = NULL;
    client->next = NULL;
    return client;
}

// 在链表末尾插入新节点
void append(clientList* list,int  connfd,struct sockaddr_in clientData) {
    newClient* client = createNewClient(connfd,clientData);
    if (!list->head) {
        list->head = client;
        list->tail = client;
    } else {
        list->tail->next = client;
        client->prev = list->tail;
        list->tail = client;
    }
    list->total ++;
}

// 在链表头部插入新节点
void prepend(clientList* list, int connfd,struct sockaddr_in clientData) {
    newClient* client = createNewClient(connfd,clientData);
    if (!list->head) {
        list->head = client;
        list->tail = client;
    } else {
        client->next = list->head;
        list->head->prev = client;
        list->head = client;
    }
    list->total ++;
}

// 按顺序插入新节点
void insertInOrder(clientList* list, int connfd,struct sockaddr_in clientData) {
    newClient* client = createNewClient(connfd,clientData);
    if (!list->head || list->head->connfd >= connfd) {
        // 插入到头部
        prepend(list,connfd,clientData);
        free(client); 
        return; 
    }
    
    newClient* current = list->head;
    while (current->next && current->next->connfd < connfd) {
        current = current->next;
    }
 
    // 插入到current之后
    client->next = current->next;
    if (current->next) {
        current->next->prev = client;
    } else {
        list->tail = client; // 插入到末尾，更新tail
    }
    current->next = client;
    client->prev = current;
     list->total ++;   
}

// 删除指定值的节点
void removeClient(clientList* list, int connfd) {
    newClient* current = list->head;
    while (current) {
        if (current->connfd == connfd) {
            if (current->prev) {
                current->prev->next = current->next;
            } else {
                list->head = current->next; // 删除的是头节点
            }
 
            if (current->next) {
                current->next->prev = current->prev;
            } else {
                list->tail = current->prev; // 删除的是尾节点
            }
 
            free(current);
            return;
        }
        current = current->next;
    }
        list->total --;
}

// 查找指定值的节点
newClient* findClient(const clientList* list, int connfd) {
    newClient* current = list->head;
    while (current) {
        if (current->connfd == connfd) {
            return current;
        }
        current = current->next;
    }
    return NULL; // 未找到匹配的节点
}

// 遍历链表并打印客户端数值
void printList(const clientList* list) {
    newClient* current = list->head;
    while (current) {
        printf("connfd = %d",current->connfd);
        current = current->next;
    }
    printf("\n");
}
